#!/bin/sh
#
# 3g-action                     This script do a job for 3g ppp link
#

# From AUTOCONF
prefix=
exec_prefix=${prefix}

# Paths to programs
UVM=/usr/bin/uvm
UO_PATH=/usr/uo
CONF_PATH=/etc/ppp
EXEC_PATH=/usr/sbin

start() {
	
	VENDORID=`rdcsman 0x801e1400 u16`
	PRODUCTID=`rdcsman 0x801e1500 u16`
	
	if [ $VENDORID -eq 2996 ] ; then
		if [ $PRODUCTID -eq 771 ] || [ $PRODUCTID -eq 2829 ] ; then
			echo -e "Bringing up 3G DHCP link...\n" > /tmp/amandadhcp
			
			rndis-action start
		fi
	else
		echo -e "Bringing up 3G PPP link...\n"
		if [ $CNT_FORCE -eq 1 ] ; then    #amanda-add090821
			#rewrite connect manual force enable
			3g-stop
		fi                                #amanda-090821

		$UVM $UO_PATH/3g.conf.uo > $CONF_PATH/3g.conf
		$UVM $UO_PATH/3g_chat.uo > $CONF_PATH/3g_chat
		$UVM $UO_PATH/pap-secrets.uo > $CONF_PATH/pap-secrets
		$UVM $UO_PATH/chap-secrets.uo > $CONF_PATH/chap-secrets
		chmod 600 /etc/ppp/*-secrets

		if [ $CNT_FORCE -eq 1 ] ; then        #amanda-add090821
			#rewrite connect manual force disable
			wrcsman "0x8001000E 0x00"
		fi                                    #amanda-090821

		if [ $VENDORID -eq 2800 ] && [ $PRODUCTID -ne 26112 ]; then
	#	if [ $VENDORID -eq 2800 ]; then
			3g-hso-start
		else
			3g-start
		fi

		if [ $? = 0 ] ; then
			exit 0
		else
			wrcsman "0x801e0700 0x03 && \
					0x80010007 0x03"
			exit 1
		fi
	fi
}

stop() {

	VENDORID=`rdcsman 0x801e1400 u16`
    PRODUCTID=`rdcsman 0x801e1500 u16`

    if [ $VENDORID -eq 2996 ] && [ $PRODUCTID -eq 771 ]; then
        echo -e "Shutting down 3G DHCP link...\n"
        rndis-action stop
    else
		local err; err=0
		echo -e "Shutting down 3G PPP link...\n"

		if [ $VENDORID -eq 2800 ] && [ $PRODUCTID -ne 26112 ]; then
	#	if [ $VENDORID -eq 2800 ]; then
			3g-hso-stop > /dev/null 2>&1
		else
			3g-stop > /dev/null 2>&1
		fi
	#	3g-stop > /dev/null 2>&1
		err=$?

		wrcsman "0x801e0200 0x00 && \
				0x801e0300 0x00 && \
				0x801e0400 0x00 && \
				0x801e0500 0x00 && \
				0x801e0600 0x00 && \
				0x801e0100 0x00 && \
				0x801e0700 0x00"

		WANTYPE=`rdcsman 0x00010003 u16`
		AUTOBAK=`rdcsman 0x001e000d u16`
		if [ $AUTOBAK -eq 0 ] && [ $WANTYPE -eq 16 ]; then
			wrcsman "0x80010002 0x00 && \
					0x80010003 0x00 && \
					0x80010004 0x00 && \
					0x80010005 0x00 && \
					0x80010006 0x00 && \
					0x80010007 0x00"
		fi

		PROFILE=`rdcsman 0x001e0030 u16`
		if [ $PROFILE -eq 10 ]; then
			send_ATcmd /dev/modem2 'ATH'
		fi

		if [ $PROFILE -eq 15 ]; then
			send_ATcmd /dev/modem2 'ATH'
		fi

		if [ $VENDORID -eq 6610 ] && [ $PRODUCTID -ne 112 ]; then
			send_ATcmd /dev/modem2 'ATH'
		fi

		exit $err
	fi
}

status() {
	3g-status
}


usage() {
	echo "$0 [start|stop|restart|status]"
	exit 1
}

# +++++++++++++++ main ++++++++++++++++++++++ 
CNT_FORCE=`rdcsman 0x8001000e u16`        #amanda-add090821
[ -z "$1" ] && usage;

case "$1" in
	start)		start ;;
	stop)		stop ;;
	restart)	stop; start;;
	status)		status ;;
	*)			usage;;
esac

exit 0
